<!DOCTYPE html>
<html class="page-fill">
<head>
    <meta charset="UTF-8">
    <title>OA办公系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!--    <meta name="keywords" content="ok-admin v2.0,ok-admin网站后台模版,后台模版下载,后台管理系统模版,HTML后台模版下载">-->
<!--    <meta name="description" content="ok-admin v2.0，顾名思义，很赞的后台模版，它是一款基于Layui框架的轻量级扁平化且完全免费开源的网站后台管理系统模板，适合中小型CMS后台系统。">-->
    <link rel="shortcut icon" href="/resource/images/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/resource/css/oksub.css"/>
</head>
<body class="page-fill">
<div class="page-fill" id="login">
    <form class="layui-form" method="post">
        <div class="login_face"><img src="/resource/images/logo.png"></div>
        <div class="layui-form-item input-item">
            <label for="username">用户名</label>
            <input type="text" lay-verify="required" name="username" placeholder="请输入账号" autocomplete="off" id="username" class="layui-input">
        </div>
        <div class="layui-form-item input-item">
            <label for="password">密码</label>
            <input type="password" lay-verify="required|password" name="password" placeholder="请输入密码" autocomplete="off" id="password" class="layui-input">
        </div>
        <div class="layui-form-item input-item captcha-box">
            <label for="captcha">验证码</label>
            <input type="text" lay-verify="required" name="vercode" placeholder="请输入验证码" autocomplete="off" id="captcha" maxlength="4" class="layui-input">
            <div class="img ok-none-select" id="captchaImg">
                <img id="randImage" src="/verifyServlet?1">
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-block" lay-filter="login" lay-submit="">登录</button>
        </div>
        <div class="login-link">
            <a href="register.html">注册</a>
            <a href="forget.html">忘记密码?</a>
        </div>
    </form>
</div>
<!--js逻辑-->
<script src="/resource/lib/layui/layui.js"></script>
<script>
    layui.use(["form", "okGVerify", "okUtils", "okLayer"], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let okGVerify = layui.okGVerify;
        let okUtils = layui.okUtils;
        let okLayer = layui.okLayer;

        /**
         * 验证码
         */
        // let verifyCode = new okGVerify("#captchaImg");
        function vcodeFun(){
            $("#randImage").attr('src', '/verifyServlet?'+ Math.random());
        }
        /**
         * 数据校验
         */
        form.verify({
            password: [/^[\S]{6,12}$/, "密码必须6到12位，且不能出现空格"],
            captcha: function (val) {
                if (verifyCode.validate(val) != "true") {
                    // return verifyCode.validate(val)
                }
            }
        });
        $('#captchaImg').click(function () {
            vcodeFun();
        });

        /**
         * 表单提交
         */
        form.on("submit(login)", function (fromdata) { //data包含提交表单的参数
            // console.log(fromdata.field);
            layui.$.ajax({
                url:"/UserServlet?method=Login",
                data:fromdata.field,
                type:"POST",
                dataType:'json',
                success:function (res) {
                    console.log(res);
                    if (res.code==200){
                        layui.layer.msg("登录成功", {icon: 1});
                        location.href=res.redirect_url; //index
                    }else {
                        layui.layer.msg(res.message, {icon: 5});
                        vcodeFun();
                    }
                }
            });
            return false;
        });

        /**
         * 表单input组件单击时
         */
        $("#login .input-item .layui-input").click(function (e) {
            e.stopPropagation();
            $(this).addClass("layui-input-focus").find(".layui-input").focus();
        });

        /**
         * 表单input组件获取焦点时
         */
        $("#login .layui-form-item .layui-input").focus(function () {
            $(this).parent().addClass("layui-input-focus");
        });

        /**
         * 表单input组件失去焦点时
         */
        $("#login .layui-form-item .layui-input").blur(function () {
            $(this).parent().removeClass("layui-input-focus");
            if ($(this).val() != "") {
                $(this).parent().addClass("layui-input-active");
            } else {
                $(this).parent().removeClass("layui-input-active");
            }
        })
    });
</script>
</body>
</html>
